package com.handinglian.common.dto;

import com.github.pagehelper.PageInfo;
import com.handinglian.common.utils.FastJsonUtil;
import lombok.Data;

import java.util.List;

/**
 * 分页数据封装类
 * Created by macro on 2019/4/19.
 */
@Data
public class CommonPage<T> {
    /**
     * 当前页
     */
    private Integer pageNum;

    /**
     * 每页的数量
     */
    private Integer pageSize;

    /**
     * 总页数
     */
    private Integer pages;

    /**
     * 总记录数
     */
    private Long total;

    /**
     * 结果集
     */
    private List<T> list;

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        CommonPage<T> result = new CommonPage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setPages(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
    * 将PageHelper分页后的list转为分页信息
    * @author pxl
    * @param list 数据列表
    * @param clazz 期望转成的类型
    * @return com.pxl.miaosha.common.api.CommonPage<V>
    * @date 2019/9/9 14:15
    */
    public static <K,V> CommonPage<V> restPage(List<K> list, Class<V> clazz) {
        CommonPage<V> result = new CommonPage<>();
        PageInfo<K> pageInfo = new PageInfo<>(list);
        result.setPages(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(FastJsonUtil.ListToList(pageInfo.getList(), clazz));
        return result;
    }
}
